<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt=""> <!--logo-->
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout()">退出</el-button>
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px': '200px' ">
                <div class="toggle-button" @click="isCollapse=!isCollapse">
                    |||
                </div>
                <el-menu 
                    background-color="#545c64" 
                    text-color="#fff" 
                    active-text-color="#409EFF" 
                    :unique-opened="true" 
                    :collapse='isCollapse'
                    :collapse-transition='false'
                    :router="true"
                    :default-active="activePath">
                    <!-- 一级菜单，用v-for遍历从后端接口那拿到的数据 -->
                    <el-submenu :index="i.id.toString()" v-for="i in menuList" :key="i.id" >
                        <template slot="title">
                            <i :class="iconObj[i.id]"></i> <!--图标-->
                            <span>{{i.authName}}</span>
                        </template>
                        <!-- 二级菜单,用v-for遍历从后端接口那拿到的数据 -->
                        <el-menu-item :index=" '/' + j.path " v-for="j in i.children" :key="j.id" @click="saveNavState('/' + j.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i> <!--图标-->
                                <span>{{j.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容 -->
            <el-main>
                <!-- 路由占位 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    name:"Home",
    data() {
        return {
            // 左侧菜单数据
            menuList:[],
            // 图标，通过id作为key，图标作为value
            iconObj:{
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao'
            },
            isCollapse:false,
            activePath:''
        }
    },
    methods:{
        // 退出登录的逻辑是先清空token，然后重定向到login页面
        logout(){
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有菜单
        async getMenuList(){
            const {data:result}=await this.$http.get('menus')
            if(result.meta.status !== 200){
                return this.$message.error(result.meta.msg)
            }else{
                this.menuList=result.data
            }
        },
        // 保存链接的激活状态
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath=activePath
        }
    },
    // 刚加载home页面时就要获取左侧菜单（接菜单的API）
    created(){
        this.getMenuList()
        this.activePath=window.sessionStorage.getItem('activePath')
    }
}
</script>

<style lang='less' scoped>
    .home-container{
        height: 100%;
    }
    .el-header{
        background-color: #373d3f;
        display: flex;
        justify-content: space-between;
        padding-left: 10px;
        align-items: center;
        color: white;
        font-size: 20px;
        > div{
            display: flex;
            align-items: center;
            span{
                margin-left: 15px;
            }
        }
    }
    .el-aside{
        background-color: #333744;
        .el-menu{
            border-right: 0;
        }

    }
    .el-main{
        background-color: #eaedf1;
    }
    .toggle-button{
        background-color: #333744;
        font-size: 10px;
        line-height: 24px;
        color:white;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>